<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hdholidaycustomermodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'customer_status' => 'customer_status',
        'sex' => 'sex',
        'customer_time_create_start' => 'customer_time_create >=',
        'customer_time_create_end' => 'customer_time_create <=',
        'customer_time_lottery_start' => 'customer_time_lottery >=',
        'customer_time_lottery_end' => 'customer_time_lottery <=',
        'openid' => 'openid',
        'gift_type' => 'lenovo_business_customer.gift_type',
        'gift_status' => 'lenovo_business_customer.gift_status',
        'customer_id' => 'lenovo_business_customer.customer_id',
        'gift_code' => 'gift_code',
    );

    public static $likeField = array(
        'nickname' => 'nickname',
        'province' => 'province',
        'city' => 'city',
        'customer_name' => 'lenovo_business_customer.customer_name',
        'customer_phone' => 'lenovo_business_customer.customer_phone',
        'customer_car_number' => 'lenovo_business_customer.customer_car_number',
        'create_source' => 'create_source',
        'validate_source' => 'validate_source',
        'lottery_source' => 'lenovo_business_customer.lottery_source',
        'h5_type' => 'h5_type',
    );

    public static $sortField = array(
        'lottery_times' => 'lottery_times',
        'money' => 'money',
        'gift_type' => 'lenovo_business_customer.gift_type',
    );

    public static $historySearchField = array(
        'customer_id' => 'lenovo_business_lottery_history.customer_id',
        'gift_type' => 'lenovo_business_lottery_history.gift_type',
        'gift_status' => 'lenovo_business_lottery_history.gift_status',
        'lottery_time_time_create_start' => 'lottery_time_time_create >=',
        'lottery_time_time_create_end' => 'lottery_time_time_create <=',
        'lottery_time_time_active_start' => 'lottery_time_time_active >=',
        'lottery_time_time_active_end' => 'lottery_time_time_active <=',
        'type' => 'type',
    );

    public static $historyLikeField = array(
        'nickname' => 'nickname',
        'code' => 'code',
        'customer_name' => 'lenovo_business_lottery_history.customer_name',
        'customer_phone' => 'lenovo_business_lottery_history.customer_phone',
        'customer_car_number' => 'lenovo_business_lottery_history.customer_car_number',
        'user_name' => 'user_name',
        'create_source' => 'lenovo_business_lottery_history.create_source',
        'validate_source' => 'lenovo_business_lottery_history.validate_source',
        'lottery_source' => 'lenovo_business_lottery_history.lottery_source',
    );

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getCustomerListSection($assist){
        if(!empty($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('lenovo_business_customer');
        // $this->db->join('user','lenovo_business_customer.shop_id = user.user_id','left');
        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(!empty($assist['where'])) $this->db->where($assist['where']);
        if(!empty($assist['like'])) $this->db->like($assist['like']);
        if(!empty($assist['where_sql'])) $this->db->where($assist['where_sql'],NULL,FALSE);
        if(!empty($assist['where_in'])){
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    return array(array(),0);
                }
            }
        }
        if(!empty($assist['pagination'])) {
            $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);
        }

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('lenovo_business_customer.customer_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) {
            $count = count($list);
        }

        return array($list,$count);
    }


    public function getCustomerById($customer_id){
        if(!is_array($customer_id)) $customer_id = array($customer_id);
        if(empty($customer_id)) return array();

        $this->db->from('lenovo_business_customer');
        $this->db->where_in('lenovo_business_customer.customer_id',$customer_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }


    public function getCustomer($customer_id){
        $this->db->from('lenovo_business_customer');
        $this->db->where('lenovo_business_customer.customer_id',$customer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }


    public function getCustomerByOpenid($openid,$select = ''){
        if (!empty($select)) $this->db->select($select);
        $this->db->from('lenovo_business_customer');
        $this->db->where('openid',$openid);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

    public function getLotteryHistoryListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('lenovo_business_lottery_history');
        if(!isset($assist['nojoin'])) {
            $this->db->join('lenovo_business_customer','lenovo_business_customer.customer_id = lenovo_business_lottery_history.customer_id','left');
        }
        if(isset($assist['join_user'])) {
            $this->db->join('user','lenovo_business_lottery_history.user_id = user.user_id','left');
        }

        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if(!empty($assist['where_sql'])) $this->db->where($assist['where_sql'],NULL,FALSE);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }

		if(!empty($assist['pagination'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('lottery_history_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) $count = count($list);

        return array($list,$count);
    }

    public function getLotteryHistoryGift($customer_id){
        $this->db->from('lenovo_business_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->where('gift_type >',0);
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }

    public function getLotteryHistoryByCode($code){
        $this->db->from('lenovo_business_lottery_history');
        $this->db->where('code',$code);
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }

    public function getLotteryHistoryList($customer_id) {
        $this->db->from('lenovo_business_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $query = $this->db->get();
        $result = $query->result_array();

        return $result;
    }

    public function getCustomerCount() {
        $this->db->from('lenovo_business_customer');
        $count = $this->db->count_all_results();

        return $count;
    }
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createCustomer($info){
        $db_debug = $this->db->db_debug;
        // $this->db->db_debug = FALSE;
        $m = $this->db->insert('lenovo_business_customer',$info);
        $customer_id = $this->db->insert_id();
        $this->db->db_debug = $db_debug;

        return $m ? $customer_id : 0;
    }

    public function createLotteryHistory($info) {
        $db_debug = $this->db->db_debug;
        $this->db->db_debug = FALSE;
        $m = $this->db->insert('lenovo_business_lottery_history',$info);
        $id = $this->db->insert_id();
        $this->db->db_debug = $db_debug;

        return $m ? $id : 0;
    }

/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editCustomer($info) {
        $this->db->where('customer_id',$info['customer_id']);
        unset($info['customer_id']);
        $this->db->update('lenovo_business_customer',$info);
    }

    public function updateCustomerLotteryCount($customer_id) {
        $this->db->query('update {PRE}lenovo_business_customer a set lottery_times=(select count(*)
                from {PRE}lenovo_business_lottery_history where customer_id=a.customer_id) WHERE a.customer_id='.$customer_id);
    }

    public function editLotteryHistory($info) {
        $this->db->where('lottery_history_id',$info['lottery_history_id']);
        unset($info['lottery_history_id']);
        $this->db->update('lenovo_business_lottery_history',$info);
    }

/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkHasLotteryGift($customer_id){
        $this->db->from('lenovo_business_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->where('gift_type >',0);
        $count = $this->db->count_all_results();

        return ($count > 0);
    }

    public function checkLotteryGiftAvailable($customer_id,$type) {
        $this->db->from('lenovo_business_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->where('type',$type);
        $count = $this->db->count_all_results();

        return ($count == 0);
    }

}
